import 'package:flutter/material.dart';
import 'package:get/route_manager.dart';
import 'package:short_drama/utils/colors.dart';
import 'package:short_drama/utils/http_util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class CustomDialog extends StatelessWidget {
  final String type;
  final int dou;
  final int value;
  final String title;
  final String account_no;
  final VoidCallback confirm;
  CustomDialog({
    super.key,
    required this.type,
    required this.dou,
    required this.value,
    required this.title,
    required this.account_no,
    required this.confirm,
  });
  final TextEditingController _codecontroller = TextEditingController(text: '');
  int lastCount = 0;

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    final itemWidth = (width - 44 - 8) / 3;
    lastCount = dou;

    return StatefulBuilder(
      builder: (context, setState) {
        return Dialog(
          backgroundColor: Colors.transparent,
          child: GestureDetector(
            onTap: () {
              FocusScope.of(context).unfocus();
            },
            child: Container(
              padding: EdgeInsets.all(22),
              decoration: BoxDecoration(
                //color: Colors.black,
                borderRadius: BorderRadius.circular(12),
                image: DecorationImage(
                  image: AssetImage('assets/images/icon_with_draw.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: AspectRatio(
                aspectRatio: 3 / 3.4,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      margin: EdgeInsets.all(4),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: SizedBox(
                              height: 30,
                              width: itemWidth,
                              child: Text(
                                title,
                                textAlign: TextAlign.right,
                                style: TextStyle(
                                  fontSize: 20,
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                          GestureDetector(
                            onTap: () {
                              Navigator.pop(context);
                            },
                            child: Expanded(
                              child: SizedBox(
                                height: 30,
                                width: itemWidth,
                                child: Text(
                                  '关闭',
                                  textAlign: TextAlign.right,
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Colors.white,
                                    fontWeight: FontWeight.normal,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(height: (title == '提现') ? 70 : 22),
                    Container(
                      padding: EdgeInsets.only(left: 16, right: 16),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        color: const Color(0x80FFFFFF),
                      ),
                      height: 44,
                      child: Row(
                        children: [
                          Expanded(
                            child: TextField(
                              controller: _codecontroller,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: (title == '兑换') ? '' : '',
                                hintStyle: TextStyle(
                                  color: Colors.white,
                                  fontSize: 17,
                                ),
                              ),
                              textAlign: TextAlign.left,
                              keyboardType: TextInputType.number,
                              onAppPrivateCommand: (action, data) {},
                              onChanged: (count) {
                                if (title == '兑换') {
                                  if (count == '') {
                                    setState(() {
                                      lastCount = dou;
                                    });
                                  }
                                  int? number = int.tryParse(count);
                                  if (number != null) {
                                    if (value > 0) {
                                      if (number > value) {
                                        EasyLoading.showToast('请输入正确兑换积分');
                                      } else {
                                        int newdou = number! * 100;
                                        if (newdou > dou) {
                                          EasyLoading.showToast('请输入正确兑换积分');
                                        } else {
                                          setState(() {
                                            lastCount = dou - newdou;
                                          });
                                        }
                                      }
                                    } else {
                                      EasyLoading.showToast('无可兑换积分');
                                    }
                                  }
                                }
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(height: 18),
                    Visibility(
                      visible: (title == '提现' ? true : false),
                      child: Container(
                        padding: EdgeInsets.only(left: 16, right: 16),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        height: 34,
                        child: Expanded(
                          child: Row(
                            children: [
                              Text(
                                '提示：30',
                                textAlign: TextAlign.left,
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 17,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Visibility(
                      visible: (title == '提现' ? false : true),
                      child: Container(
                        height: 120,
                        padding: EdgeInsets.all(25),
                        decoration: BoxDecoration(
                          color: ClientColors.ff0b9970,
                          borderRadius: BorderRadius.circular(12),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Row(
                              children: [
                                Text(
                                  '剩余数量',
                                  style: TextStyle(
                                    fontSize: 16,
                                    color: Colors.white,
                                  ),
                                ),
                                Expanded(child: Container()),
                                Text(
                                  '${dou}',
                                  style: TextStyle(
                                    fontSize: 16,
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                Text(
                                  '兑换后数量',
                                  style: TextStyle(
                                    fontSize: 16,
                                    color: Colors.white,
                                  ),
                                ),
                                Expanded(child: Container()),
                                Text(
                                  '${lastCount}',
                                  style: TextStyle(
                                    fontSize: 16,
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                    Expanded(child: Container()),
                    GestureDetector(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                            child: Container(
                              decoration: BoxDecoration(
                                color: ClientColors.ff0b9970,
                                borderRadius: BorderRadius.circular(22),
                              ),
                              height: 44,
                              child: GestureDetector(
                                onTap: () {
                                  _submitAction();
                                },
                                child: Column(
                                  children: [
                                    Expanded(child: Container()),
                                    Text(
                                      '确定',
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        fontSize: 16,
                                        color: Colors.white,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                    Expanded(child: Container()),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  void _submitAction() async {
    int? number = int.tryParse(_codecontroller.text);
    if (number != null) {
      int newdou = number! * 100;
      if (title == '兑换') {
        if (number > value || newdou > dou) {
          EasyLoading.showToast('请输入正确积分数量');
        } else {
          await ExtractViewModel().convertdialog(
            account_type: type,
            exchange_money: _codecontroller.text ?? '',
          );
          confirm();
          Get.back();
        }
      } else if (title == '提现') {
        if (number > value) {
          EasyLoading.showToast('请输入正确数量');
        } else {
          String money = '${number * 100}';
          await ExtractViewModel().extractdialog(
            account_id: account_no,
            account_type: type,
            apply_money: money,
            transfer_type: 'bank',
          );
          confirm();
          Get.back();
        }
      }
    } else {
      EasyLoading.showToast('请输入积分数量');
    }
  }
}

class CustomGenderDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(color: ClientColors.ff0a0f12),
        height: 100,
        child: Column(
          children: [
            //Expanded(child: Container()),
            Expanded(
              child: GestureDetector(
                onTap: () {
                  Get.back();
                },
                child: Text(
                  '男',
                  style: TextStyle(fontSize: 16, color: Colors.white),
                ),
              ),
            ),
            Expanded(
              child: GestureDetector(
                onTap: () {
                  Get.back();
                },
                child: Text(
                  '女',
                  style: TextStyle(fontSize: 16, color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class CustomServiceDialog extends StatelessWidget {
  final String imageUrl;

  const CustomServiceDialog({super.key, required this.imageUrl});

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: ClientColors.ff0a0f12,
          borderRadius: BorderRadius.circular(12),
        ),
        child: AspectRatio(
          aspectRatio: 1 / 1,
          child: GestureDetector(
            onTap: () {
              Get.back();
            },
            child: Expanded(child: Image.network(imageUrl)),
          ),
        ),
      ),
    );
  }
}

class ExtractViewModel {
  Future<void> extractdialog({
    String account_id = '',
    String account_type = '',
    String apply_money = '30',
    String transfer_type = 'bank',
  }) async {
    try {
      final result = await HttpUtil().post(
        'member/cash_out/apply',
        params: {
          "account_id": account_id,
          "account_type": account_type,
          "apply_money": apply_money,
          "transfer_type": transfer_type,
        },
      );
      final dynamic responseData = result.data['data'];
      final String title = result.data['msg'];
      if (title == '提现账户不存在') {
        EasyLoading.showToast('请绑定银行卡');
      }
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
  }

  Future<void> convertdialog({
    String account_type = '',
    String exchange_money = '',
  }) async {
    try {
      final result = await HttpUtil().post(
        'member/account/exchange',
        params: {
          "exchange_money": exchange_money,
          "account_type": account_type,
        },
      );
      final dynamic responseData = result.data['data'];
      print(responseData);
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
  }
}
